import tkinter as tk
from tkinter import LabelFrame

class ListFrame(LabelFrame):
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.Gui_Show()
        self.iindex = 0
        self.columnconfigure(0,weight=1)
        self.columnconfigure(1,weight=0)
        self.rowconfigure(0,weight=1)
    def Gui_Show(self):
        scbar=tk.Scrollbar(self)
        self.lb=tk.Listbox(self,selectmode="SINGLE",yscrollcommand=scbar.set)
        scbar.config(command=self.lb.yview)
        self.lb.grid(column=0,row=0,sticky="NSWE")
        scbar.grid(column=1,row=0,sticky="NS")
        self.lb.bind('<<ListboxSelect>>',self.Gui_BindDeal)
    def Gui_BindDeal(self,e):
        if self.lb.curselection()==():
            return
        print("index{}".format(self.lb.curselection()))
        print("Click{}".format(self.lb.get(self.lb.curselection())))
        self.m_CallBackFunc(self.lb.curselection()[0],self.lb.get(self.lb.curselection()))
        
    def m_ListDataToGui(self,listData:list):
        
        self.lb.delete(0,'end')
        for idx in listData:
            self.lb.insert('end',idx)
    #----测试进度------------------------
    def m_TestListDataInsert(self,listData:list):
        '''添加测试数据'''
        self.listdata = listData
        self.listdatastate = [3]*len(self.listdata)
        self.m_TestUpdateShow()
    def m_TestStateSet(self,index,state):
        '''0:Fail 1:Pass 2:Running 3:Wait'''
        if index<len(self.listdatastate):
            self.listdatastate[index]=state
            self.m_Item_Select(index)
            self.m_TestUpdateShow()
    def m_TestUpdateShow(self):
        self.lb.delete(0,'end')
        for idx,item in enumerate(self.listdata):
            self.lb.insert('end',"[{:^3}] | {:<30}   {:<6}".format(idx,item,["Fail","Pass","Running","Wait"][self.listdatastate[idx]]))
    def m_TestClearState(self):
        self.listdatastate = [3]*len(self.listdata)
        self.m_TestUpdateShow()
    #----测试进度--------------------------
    #简单用法
    def m_JustInsert(self,itemname,state):
        self.lb.insert('end',"[{:^3}] | {:<50}   {:<6}".format(self.iindex,itemname,["Fail","Pass","Running","Wait"][state]))
        self.iindex+=1
    def m_JustClear(self):
        self.iindex=0
        self.lb.delete(0,'end')
    #简单用法

    def m_CallBackFunc(self,a,b):
        '''
        index|select
        '''
        print("{}-{}".format(a,b))
    def m_Update_Item(self,label_list:list):
        self.lb.delete(0,'end')
        for idx in label_list:
            self.lb.insert('end',"[{}] | {}".format(label_list.index(idx),idx))
    def m_Item_Select(self,num):
        self.lb.selection_clear(0,'end')
        self.lb.activate(num)
        self.lb.selection_set(num)
        self.lb.see(num)
